package com.ljx.itbbs.server.mall.event.handler;

import com.ljx.itbbs.common.constant.CacheConstants;
import com.ljx.itbbs.common.utils.RedisUtil;
import com.ljx.itbbs.server.mall.dao.CouponDao;
import com.ljx.itbbs.server.mall.event.CouponSaveEvent;
import com.ljx.itbbs.server.mall.event.OrderSubmitEvent;
import com.ljx.itbbs.server.mall.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-04-27  19:24
 */
@Component
public class CouponSaveEventHandler {

    @Autowired
    private CouponDao couponDao;

    /**
     * 保存Mysql
     * @param event
     */
    @Async
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = CouponSaveEvent.class, fallbackExecution = true)
    public void saveMysql(CouponSaveEvent event){
        couponDao.save(event.getCouponEntity());
    }

    /**
     * 保存Redis
     * @param event
     */
    @Async
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = CouponSaveEvent.class, fallbackExecution = true)
    public void saveRedis(CouponSaveEvent event){
        RedisUtil.set(CacheConstants.getKey(CacheConstants.COUPON_STOCK), event.getCouponEntity().getStock());
    }
}
